package cases.boxes 
{
	
	import flash.events.Event;
	import org.despair2D.ui.events.ManipulateEvent;
	import org.despair2D.ui.LabelButton;
	import org.despair2D.ui.PersonaState;
	import org.despair2D.ui.puppet.ImagePuppet;
	

public class ImagePuppetDemo extends PersonaState
{
	
	private const numImage:int = 25
	private const gapX:int = 70
	private const gapY:int = 70;
	
	[Embed(source = "../img.png")]private var optional:Class
	[Embed(source="../defaultImg.png")]private var defaultImg:Class
	
	
	private var imageList:Array = []
	private var urlList:Array = ['http://imgsrc.baidu.com/baike/abpic/item/a6c7d717e8257737c93d6dfe.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/a75fb6d3bccd1c74960a1637.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/b7bc4c66a9e7466baa184ce5.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/b840549012e67fb5a877a43b.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/8367d1fcefc971d6b801a011.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/14ce36d3d539b60060483bf4e950352ac75cb7ea.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/023b5bb5c9ea15cef137444bb6003af33b87b293.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/21e55823a69ae81d9822ed9a.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/310f3b1f6ea679fda7866987.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/389aa8fd9eca2b4208244d26.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/3a86813dc88dac68baa1679e.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/3b6833f56afd6076bc3109fc.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/3bc6f750ce96897e1138c244.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/4075890a88b5cc2994ca6b17.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/43e6c733fb787317ac4b5fe6.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/4d4970061b2a0f430308815c.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/6a22e824650029678744f991.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/6f4703951bcef95c7af4802f.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/9c57e3fadb66f48db48f31af.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/9dc3cf580d9ebfe1800a18a7.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/9f1011b30d3092f2d9335ace.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/a54e55fbb5983b3f024f5675.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/a583631e869f64c31bd57698.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/a6c7d717e8257737c93d6dfe.jpg',
								 'http://imda9f1f4b8961f17a21a.jpg'
								]
	
	override public function enter():void
	{
		var IP:ImagePuppet
		var i:int
		while (i < numImage)
		{
			IP = this.group.createImagePuppet(true)
			IP.move((i % 5) * gapX + 150, int(i / 5) * gapY + 160)
			imageList[i++] = IP
			IP.embed(optional)
		}
		
		var label:LabelButton
		
		label = new LabelButton('Btn_A', 'load images')
		label.move(90, 30)
		label.addEventListener(ManipulateEvent.CLICK, onLoadImage)
		this.group.addFusion(label)
		
		label = new LabelButton('Btn_A', 'Empty')
		label.move(170, 30)
		label.addEventListener(ManipulateEvent.CLICK, onEmpty)
		this.group.addFusion(label)
	}
	
	private function onLoadImage(e:ManipulateEvent):void
	{
		var IP:ImagePuppet
		var i:int
		while (i < numImage)
		{
			IP = imageList[i];
			IP.load(urlList[i], false, defaultImg).addCompleteListener(function(e:Event):void
			{
				trace(e.target.url)
			})
			i++
		}
	}
	
	private function onEmpty(e:ManipulateEvent):void
	{
		var IP:ImagePuppet
		var i:int
		while (i < numImage)
		{
			IP = imageList[i];
			IP.empty()
			i++
		}
	}
}

}